﻿using System;
using System.Collections.Generic;
using System.Text;
using POSPlus.EZBarTender.DatabaseOperations;
using System.Data.SqlClient;

namespace POSPlus.DatabaseOperations
{
	public class PrintJob : DatabaseObject
	{
		public String JobName { get; private set; }
		public Int32 Items { get; private set; }
		public string Type { get; private set; }

		public PrintJob(String connectionString)
		{
			ConnectionString = connectionString;
		}

		public PrintJob(String connectionString, String jobName, Int32 items)
		{
			ConnectionString = connectionString;
			JobName = jobName;
			Items = items;
			Type = JobName;
		}

		public static List<PrintJob> GetAll(String connectionString)
		{
			List<PrintJob> result = new List<PrintJob>();
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT * FROM POSplus_PrintJobs ORDER BY JobName",
					connection))
				{
					using (SqlDataReader reader = command.ExecuteReader())
					{
						while (reader.Read())
						{
							PrintJob job = new PrintJob(connectionString,
								FromDB<String>(reader["JobName"]),
								FromDB<Int32>(reader["Items"]));
							result.Add(job);
						}
						reader.Close();
					}
				}
				connection.Close();
			}
			return result;
		}

		private JobType ParseJobType(String jobName)
		{
			if (jobName == "")
				return JobType.None;

			String type = jobName;
			if (jobName.Split(' ').Length > 0)
				type = jobName.Split(' ')[0];

			switch (type)
			{
				case "SB": return JobType.SmartBuy;
				case "SD": return JobType.SpecialDeal;
				case "NEL": return JobType.NowEvenLower;
				case "SS": return JobType.SuperSale;
				case "BB": return JobType.BonusBucks;
				default: throw new InvalidCastException(type + " is not a valid cast to JobType.");
			}
		}

		private JobType ParseJobSize(String jobName)
		{
			if (jobName == "")
				return JobType.None;

			String size = jobName;
			if (jobName.Split(' ').Length > 1)
				size = jobName.Split(' ')[1];

			switch (size)
			{
				case "S1UP": return JobType.SmallOneUp7x11;
				case "M1UP": return JobType.MediumOneUp8x11;
				case "1UP": return JobType.OneUp13x10;
				case "L1UP": return JobType.OneUp14x11;
				case "2UP": return JobType.TwoUp;
				case "4UP": return JobType.FourUp;
				default: throw new InvalidCastException(size + " is not a valid cast to JobSize.");
			}
		}

		protected override void PopulateDefault()
		{
			throw new NotImplementedException();
		}

		public override Boolean Create()
		{
			throw new NotImplementedException();
		}

		public override Boolean Update()
		{
			throw new NotImplementedException();
		}

		public override Boolean Delete()
		{
			throw new NotImplementedException();
		}
	}
}
